/**
 * Created by Administrator on 2017/3/13.
 */
//$("#footer").load("common/footer.html");
var page=1;//下拉加载的页码
var cid='';//分类id
$(function() {

    // 黑名单添加
    $.get(member_blacklist_add,function (res) {
        var response = JSON.parse(res);
        if(response.status) {
        }
    })
    //判断是否有搜索内容
    var searchTxt=getQueryString('keyword');
    if(searchTxt){
        //$('.sliderImage,.fenleiBImg,.fenleiSImg').css('display','none');

        $('#searchText span').html(searchTxt);
        ajaxGoodsList(1, 6, '','',searchTxt);
    }else {
        //加载轮播图
        $.get(commonURL + 'op=banner', function (data) {
            var res = JSON.parse(data);
            var liHtml = '';
            var numHtml = '';
            for (var i = 0; i < res.length; i++) {
                liHtml += "<li><img src=" + res[i].thumb + " style='width:100%;'></li>";
                if (i == 0) {
                    numHtml += '<span class="curr"></span>'
                } else {
                    numHtml += '<span></span>'
                }
            }
            $('#sliderImg>ul').html(liHtml);
            $('#sliderNum').html(numHtml);

            new Swipe($('#sliderImg')[0], {
                speed: 500,
                auto: 3000,
                callback: function () {
                    var lis = $("#sliderNum").children();
                    lis.removeClass("curr").eq(this.index).addClass("curr");
                }
            });
        });

        //加载商品分类图
        $.get(commonURL + 'op=category', function (data) {
            var res = JSON.parse(data);
            console.log(res);
            var liHtmlS = '',liHtmlB = '';
            for (var i = 0; i < res.length; i++) {
                if(res[i].show_type=='1'){
                    liHtmlB += '<a onclick=fenleiSImg(' + res[i].id + ',this) href="##"><img src=' + res[i].smallthumb + ' alt=""/></a>';
                }else{
                    liHtmlS += '<a onclick=fenleiSImg(' + res[i].id + ',this) href="##"><img src=' + res[i].thumb + ' alt=""/><span>' + res[i].name + '</span></a>';
                }
            }
            if(liHtmlB==''){
                $('.fenleiBImg').css('display','none');
            }
            if(liHtmlS==''){
                $('.fenleiSImg').css('display','none');
            }
            $('.fenleiSImg').html(liHtmlS);
            $('.fenleiBImg').html(liHtmlB);
        });

        //加载首页产品
        ajaxGoodsList(1, 6, cid);
    }
});

//我的中心弹窗
$(".top .person").click(function () {
    $(".top .desc").slideToggle();
    $('.dialog-mask').show();
});

// 我的订单
$('.desc .desc-order').click(function () {
    location.href = $(this).attr('data-href');
});

// 我的中心
$('.desc .desc-member').click(function () {
    location.href = $(this).attr('data-href');
});
// 我的客服
$('.desc .desc-service').click(function () {
    $('.dialog-mask').css({
        'background-color': 'rgba(7, 17, 47, .6)',
        'z-index': '20'
    })
        .show();
    $('.dialog-service').show();
})

// 遮罩
$('.dialog-mask').click(function () {
    $('.dialog-mask').css({
        'background-color': 'rgba(0, 0, 0, 0)',
        'z-index': '0',
    })
    $('.desc:visible').hide();
    $('.dialog-mask').hide();
    $('.dialog-service').hide();
});

//弹窗
$('.top li:nth-child(2)').click(function(e){
    e.stopPropagation(e);//阻止冒泡
    //$('.top li:nth-child(2)>a').css('transform','rotate(90deg)');
    //$('.sortlight').css('display','block');
    $('.sortlight').slideToggle();
});
document.onclick=function(){
    $('.top li:nth-child(2)>a').css('transform','rotate(0deg)')
    $('.sortlight').css('display','none');
};

//滚动加载
$(document.body).infinite();//初始化
var loading = false;  //状态标记
$(document.body).infinite().on("infinite", function() {
    if(loading) return;
    page++;
    loading = true;
    $('.loadready').html('正在玩命加载中...');
    setTimeout(function() {
        ajaxGoodsList(page,6,cid,'append');
        loading = false;
        $('.loadready').html('');
    }, 700);   //模拟延迟
});

//$(document.body).destroyInfinite();//停止加载
$('#searchBar').click(function () {     //解决搜索取消按钮的display不起作用的BUG
    $('#searchCancel').css({
        'position':'relative',
        'top': '0'
    })
}) 
    
//搜索
$('.searchSucc').click(function(){
    $('#searchCancel').css({
        'position':'relative',
        'top': '0'
    })
    var searchTxt=$('#searchInput').val();
    if(searchTxt==""){
        return false;
    }
    location.href=searchUrl+"keyword="+searchTxt;
});


//分类小图
function fenleiSImg(id,self){
    cid=id;
    if($(self).find('span').hasClass('active')){return false;}
    $(document.body).infinite();//初始化
    $('.fenleiSImg span').removeClass('active');
    $(self).children('span').addClass('active');
    page=1;
    ajaxGoodsList(page,6,cid);
}

//跳转到详情页
function gotoDetail(self){
    var id=$(self).attr('data-id');
    location.href=detailurl+"id="+id;
}

//加载商品列表
function ajaxGoodsList(pagenum,pagesize,cid,append,keyword){
    $.get(
        commonURL+'op=goods_list',
        {
            page:pagenum,
            pagesize:pagesize,
            cid:cid,
            keyword:keyword
        },
        function(data){
            var res=JSON.parse(data);
            console.log(res);
            var listHtml='';
            for(var i=0;i<res.list.length;i++){
                var free = Number(res.list[i].oprice);
                var apply_start = res.list[i].apply_start;
                var apply_end = res.list[i].apply_end;
                apply_start = window.formatDate(apply_start);
                apply_end = window.formatDate(apply_end);
                if(free === 0){
                    var price = '<div class="data_price"><span>免费</span>';   
                }else{
                    var price = '<div class="data_price">￥<b>'+res.list[i].oprice+'</b><s>￥'+res.list[i].mprice+'</s>';
                }
                var deliverytype=res.list[i].deliverytype=='1'?'上门':'到店'; 
                listHtml+='<div class="lists_data" onclick="gotoDetail(this)" data-option='+res.list[i].hasoption+' data-id='+res.list[i].id+'><div class="data_img"><img src='+res.list[i].gimg+' alt=""/></div>'
                +'<div><div class="data_title"><b>'+res.list[i].gname+'</b></div>'
                + price
                // +'<i class="iconal-yuebiao" onclick="yuebiaoClick(this,event)" data-gnum='+res.list[i].gnum+' data-id='+res.list[i].id+' data-type='+res.list[i].deliverytype+' data-params='+JSON.stringify(res.list[i].params)+' data-notice='+res.list[i].notice+'></i>'
                +'</div><div class="data_time"><ul><li><span>报名时间:'+'<br/>'+apply_start+'至'+apply_end+'</span></li>'
                +'<li style="float:right;margin-right: 10px;"><span></span></li></ul></div></div></div>'
            }
            if(append=='append'){
                $(".lists").append(listHtml);
            }else {
                $(".lists").html(listHtml);
            }
            if(res.list.length<1){
                //$.toptip('全部完毕', 'success');
                //$.toast('数据加载完毕','success');
                $('.loadready').html('数据加载完毕').css('color','#aaa');
                $(document.body).destroyInfinite();//停止加载
                return false;
            }
        }
    )
}




    //取消按钮操作
$('.closeTJ').click(function() {
    $(this).parent().parent().slideUp();
});
$('.iconal-roundclosefill').click(function() {
    $(this).parent().parent().fadeOut();
});
$('.btn-box .cancel').click(function() {
    $(this).parent().parent().parent().fadeOut();
});
//获取门店列表
function storesCH(e) {
    $.get(
        commonURL + 'op=store', {
            id: $('#choose').attr('data-id')
        },
        function(res) {
            if (res == 'null') {
                $.toast('暂无课程', 'cancel');
                return false;
            } else {
                var data = JSON.parse(res);
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    html += "<li data-id=" + data[i].id + "><i onclick='chooseI(this)' class='weui-icon-success'></i><p class='kuan1'>" + data[i].storename + "</p><p class='kuan2'>" + data[i].address + "</p></li>"
                }
                $('#chooseMendian').slideDown();
                $('#chooseMendian>ul').html(html).attr('data-class', '.stores' + $(e).attr('data-id'));
            }
        }
    );
}
// 选择门店
function chooseI(a) {
    $('#chooseMendian li i').removeClass('active');
    $(a).addClass('active');
}
// 提交--门店
$('#chooseMendian .storeTJ').click(function(e) {
    $(e).addClass('active');
    $('#show-actions-times p').html('请选择时间');
    //        $('#show-actions-fuwuJS p').html('请选择技师');
    var store = $('#chooseMendian li i');
    for (var i = 0; i < store.length; i++) {
        if ($(store[i]).hasClass('active')) {
            var p = $(store[i]).next('p');
            $('.MDorSM>a').attr('data-storesid', $(store[i]).parent().attr('data-id')).find('p').html('活动地址: ' + p.html()).attr('data-id', $(store[i]).parent().attr('data-id'));
            $('#chooseMendian').slideUp();
            return false;
        }
    }
    $.toast('请选择地点', 'text');
});
var nowDate = {
    Date: Date.parse(new Date()),
    Year: new Date().getFullYear(),
    Month: new Date().getMonth()+1,
    Day: new Date().getDate()
}



//获取时间列表
function timeCH(e) {
    if($('.MDorSM p').html()=='请选择地点'){
       $.toast('请先填写课程','text');
       setTimeout(function(){
           $('.MDorSM>a').click();
       },1500);
       return false;
    }
    $.ajax({
        url: date_push,
        type: 'GET',
        data: { 
            id: 7754  
        },
        success:function (res) {
            var response = JSON.parse(res);
            var _startDate = response.data.start_date;
            var _endDate = response.data.end_date;
            var _week = response.data.week;
            var _weekarr = [];
            for(var key in _week){
                _weekarr.push(key);
            }
            var _weekarrtoString = _weekarr.toString();
        // var _startDate = 1499139258000;   //模拟数据
        // var _endDate = 159910258000;

        var arrDate = [];
        var arrDateYMD = [];    //存放Y-M-R
        var weekbyYMD = [];    //存放getDay  如3
        var DateinWeek = [];  //存放周几例如： 周三
        // 时间对象
        var minDate = {
            limit: {
                startYear: new Date(_startDate).getFullYear(),
                startMonth: new Date(_startDate).getMonth(),
                startDay: new Date(_startDate).getDate(),
                endYear: new Date(_endDate).getFullYear(),
                endMonth: new Date(_endDate).getMonth(), 
                endDay: new Date(_endDate).getDate()
            }
        }
        
        function getDate(datestr){
           var temp = datestr.split("-");
           var date = new Date(temp[0],temp[1],temp[2]); 
           return date;
        }
        function trueMinYear () {
            if(nowDate.Year < minDate.limit.Year) return minDate.Year;
            return nowDate.Year;
        }
        function trueMinMonth () {
            if(nowDate.Year == minDate.limit.Year && nowDate.Month < minDate.limit.Month) return minDate.limit.Month;
            return nowDate.Month-1;
        }
        function trueMinDay () { 
            if(nowDate.Year == minDate.limit.Year && nowDate.Month < minDate.limit.Month && nowDate.Day < minDate.limit.Day) return minDate.limit.Day;
            return nowDate.Day; 
        }

        var startDateStr = trueMinYear()+'-'+trueMinMonth()+'-'+trueMinDay();
            startDateStr = startDateStr.toString();
            //todo 时间插件
        var startTime = getDate(startDateStr);//开始时间
        var endTime = getDate(_endDate);//结束时间
 
        while((endTime.getTime()-startTime.getTime())>=0){
          var year = startTime.getFullYear();
          var month = (startTime.getMonth()+1).toString().length==1?"0"+(startTime.getMonth()+1).toString():(startTime.getMonth()+1);
          var day = startTime.getDate().toString().length==1?"0"+startTime.getDate():startTime.getDate();
          var testDate = new Date(year+'-'+month+'-'+day).getDay();
          if(new RegExp(testDate).test(_weekarrtoString)){   //匹配对应的周
            arrDate.push(month+'-'+day);
            arrDateYMD.push(year+'-'+month+'-'+day);
          }
        startTime.setDate(startTime.getDate()+1);
        }
        console.log(arrDate);
        var show_day = ['周日','周一', '周二', '周三', '周四', '周五', '周六'];  
        var val = "";
        for(var k = 0; k< arrDateYMD.length;k++){
            weekbyYMD.push(new Date(arrDateYMD[k]).getDay())
        }
        for(var j = 0; j<weekbyYMD.length;j++){
            val = weekbyYMD[j];
            DateinWeek.push(show_day[val]);
        }
    if ((getQueryString('ac') == 'detail')) {
        var dataTime = JSON.parse($('div.yuebiao[data-id="' + $('#choose').attr('data-id') + '"]').attr('data-params'));
    } else {
        var dataTime = JSON.parse($('.iconal-yuebiao[data-id="' + $('#choose').attr('data-id') + '"]').attr('data-params'));
    }
 
    var htmlInWeek = ''; 
    var htmlInTime = '';
    for(var i = 0;i < arrDate.length; i++ ){  //渲染时间段范围
        htmlInWeek +='<a class="weui-navbar__item" href=#tab['+i+'] onclick="showSelectTime(this)" data-week='+weekbyYMD[i]+' data-date='+arrDateYMD[i]+'><div>' + DateinWeek[i] + '</div><div>' + arrDate[i] + '</div></a>';   
    }
    $('.choose-weui-navbar').html(htmlInWeek);
    $('.choose-weui-navbar').children().first().click();


    $('#chooseTime').attr('data-id', 'times' + $(e).parent().parent().attr('data-id')).slideDown();
    },
        error:function(){
            console.log("失败");   
        }
    })
}
//选择时间段
function choosetime(self) {
    $('#chooseTime p').removeClass('active');
    $(self).addClass('active');
}
//提交--时间
$('.timeTJ').click(function() {
    var day = $('#chooseTime a.weui-navbar__item');
    var num = 0;
    var css = $('#chooseTime').attr('data-id');
    for (var j = 0; j < day.length; j++) {
        if ($(day[j]).hasClass('weui-bar__item--on')) {
            $('#chooseTime').attr('data-day', $(day[j]).find('div:last-child').html()); //存日期
            num++;
            // var time = $($($(day[j]).attr('href') + ' p'));
            // if (time.length < 1) {
            //     $.toast('请选择预约时间段', 'text');
            //     return false;
            // }
            
                    // $('#show-actions-fuwuJS p').html('请选择技师'); //选择时间时把技师重置
//                         $('#chooseTime').slideUp();
//             for (var i = 0; i < time.length; i++) {
//                 if ($(time[i]).hasClass('active')) {
//                     var selectDate;   //选择的时间，周几
//                     var Time,startTime;//时间范围
//                     $('.weui-navbar__item').each(function (index,item) {
//                         if($(item).hasClass('weui-bar__item--on')){
//                             selectDate = $(item).find('div').last().text();
//                         }

//                     });

//                     $('.weui-tab__bd-item').each(function (index,item) {
//                         if($(item).find('p').hasClass('active')){
//                             Time = $(item).find('p').attr('data-startt');
//                         }
//                     });
//                     var startYear = new Date().getFullYear();
//                     //var startTime = `${startYear}-${selectDate} ${Time}`;
//                     startTime = startYear+' '+selectDate +' '+Time;
//                     var startTimeParse = Date.parse(startTime);
//                     startTimeParse = startTimeParse / 1000;
//                     $.get(ajaxTimecount,
//                         {
//                             gid: $('#choose').attr('data-id'),
//                             starttime: startTimeParse
//                         },
//                         function (data) {
//                             var gnum = $('#choose').attr('data-gnum');
//                             gum = Number(gnum);
//                             console.log(data,gnum);
//                             if(Number(data) > Number(gnum)){
//                                 $.toast('该时间段预约已满', 'text');
//                                 return false;
//                             }else{
//                                 $('#chooseTime').slideUp();
//                             }
//                         }
//                     )
//                     $('#chooseTime').attr('data-time', $(time[i]).html()); //存数据‘时间段’
//                     $('#show-actions-times').attr('data-day', $(day[j]).find('div:last-child').html()).
//                         attr('data-timeid', $(time[i]).attr('data-id')).attr('data-startT', $(time[i]).attr('data-startT')).
//                         attr('data-endT', $(time[i]).attr('data-endT')).find('p').
//                         html('时间: ' + $('#chooseTime').attr('data-day') + ' ' + $('#chooseTime').attr('data-time'));

//                     // $('#show-actions-fuwuJS p').html('请选择技师'); //选择时间时把技师重置
                        // $('#chooseTime').slideUp();
//                     return false;
//                 }
//             }
        }
    }
    if(num == 0) {
        $.toast('请选择预约日期', 'text');
        return false;
    }
    if($('#tab p').hasClass('active')){ 
        $('#chooseTime').attr('data-time', $('#tab p').html()); //存数据‘时间段’
    }else{
        $.toast('请选择预约时间段', 'text');
        return false;
    }
    
    $('#show-actions-times').attr('data-day', $(day[j]).find('div:last-child').html())
        // attr('data-timeid', $(time[i]).attr('data-id')).attr('data-startT', $(time[i]).attr('data-startT')).
        // attr('data-endT', $(time[i]).attr('data-endT'))
        .find('p').html('时间: ' +$('#tab p').html());
    $('#chooseTime').slideUp();
});

//筛选日期
function showSelectTime (self) {
    var html = '';
    var id  = $('#choose').attr('data-id');
    var nowTime = new Date();
    var nowYear = new Date().getFullYear();
    var nowMonth = new Date().getMonth()+1;
    console.log(nowMonth);
    nowMonth = nowMonth.toString().length == 1? '0' + (new Date().getMonth()+1) : new Date().getMonth()+1;
    var nowDay = new Date().getDate().toString().length == 1 ? '0' + new Date().getDate() : new Date().getDate();
    var nowYMD = nowYear+'-'+nowMonth+'-'+nowDay;
    var date = new Date().getFullYear()+'-'+$(self).children('div:last-child').html();     
    var idWeekstr = $(self).attr('data-week');
    var dataDate = $(self).attr('data-date');  //2017-07-05
    var  DATEcLASS = new Date(dataDate)
    console.log(dataDate);
    $.ajax({
        url: select,
        type: 'POST',
        data: {
            id: id,  
            date:date,
            weekstr: idWeekstr 
        },
        success:function (res) {
            var dataTime = JSON.parse(res);
            var Time = dataTime.data.end_time.split(':');  //时间数组   前面是小时后面是分钟   10 ： 15
            var Hours = Time.splice(0,1);
            var html = '';
            var _Date =  Date.parse(new Date(dataDate+' '+Hours+':'+Time)); 
                console.log(res);
                console.log(dataTime.data.end_time);
            if(nowYMD == dataDate){
                console.log(Date.parse(nowTime));
                console.log('-----------------------')
                console.log(Number(Date.parse(nowTime)) < Number(_Date));
                if(Number(Date.parse(nowTime)) < Number(_Date)){
                   if(Number(dataTime.data.kucun) <= 0){
                        html = '<p style="background-color: #ddd" data-startT=' + dataTime.data.start_time + ' data-endT=' + dataTime.data.end_time + ' data-id=' +
                        id + 'data-stock='+dataTime.data.stock+'>' + dataTime.data.start_time + '-' + dataTime.data.end_time + '</p>';
                   }else{
                        html = '<p data-startT=' + dataTime.data.start_time + ' data-endT=' + dataTime.data.end_time + ' data-id=' +
                        id + 'data-stock='+dataTime.data.stock+' onclick="choosetime(this)">' + dataTime.data.start_time + '-' + dataTime.data.end_time +'<span>'+dataTime.data.stock+'</span></p>';
                   } 
                }else{
                    console.log(1)
                }   
            }else{
                if(Number(dataTime.data.kucun) <= 0){
                    html = '<p style="background-color: #ddd" data-startT=' + dataTime.data.start_time + ' data-endT=' + dataTime.data.end_time + ' data-id=' +
                    id + 'data-stock='+dataTime.data.stock+'>' + dataTime.data.start_time + '-' + dataTime.data.end_time + '</p>';
                }else{
                    html = '<p data-startT=' + dataTime.data.start_time + ' data-endT=' + dataTime.data.end_time + ' data-id=' +
                    id + 'data-stock='+dataTime.data.stock+' onclick="choosetime(this)">' + dataTime.data.start_time + '-' + dataTime.data.end_time + '<span>'+dataTime.data.stock+'</span></p>';
                } 
            }

            console.log(html);
            
            if(html == ''){
                $('#tab').html('<div style="text-align: center">当天预约已满</div>')
            }else{
                $('#tab').html(html);
            }
        },
        error:function(){
            console.log("失败"); 
        }
    })
    
}
// function dateTime(day, dataTime, tab) {
//     console.log(tab)
//     var _endDate = 159910258000;
//     var html = '';
//     for (var i = 0; i < dataTime.length; i++) {
//         console.log(dataTime);
//         if ( _endDate < day&& tab == '#tab[0]') {
//             //如果是当天时间并现在时间段之前的时间段不显示
//         } else { //该时间段之后并被占用的时间段显示灰色并不可选
            // html += ('<p data-startT=' + dataTime[i].title + ' data-endT=' + dataTime[i].value + ' data-id=' +
            // dataTime[i].id + ' onclick="choosetime(this)">' + dataTime[i].title + '-' + dataTime[i].value + '</p>');
//         }
//     }
//     if (html == '') {
//         $(tab).html('<div style="text-align: center">当天预约已满</div>');
//     } else {
//         $(tab).html(html);
//     }
// }


//加载技师列表
//    function ajaxPersonnel(self){
//        if($('.MDorSM p').html()=='请填写地址信息'){
//            $.toast('请先填写地址信息','text');
//            setTimeout(function(){
//                $('.MDorSM>a').click();
//            },1500);
//            return false;
//        }
//        if($('.MDorSM p').html()=='请选择门店地址'){
//            $.toast('请先选择门店地址','text');
//            setTimeout(function(){
//                $('.MDorSM>a').click();
//            },1500);
//            return false;
//        }
//        if($('#show-actions-times p').html()=='请选择时间'){
//            $.toast('请先选择时间','text');
//            setTimeout(function(){
//                $(self).prev().click();
//            },1500);
//            return false;
//        }
//        var local=JSON.parse(sessionStorage.getItem('location'))||'';
//        var dateTime=(new Date()).getFullYear()+'-'+$('#show-actions-times').attr('data-day')+' '+$('#show-actions-times').attr('data-startt');
//        $.get(
//                salerlist,
//                {
//                    page:1,
//                    pagesize:30,
//                    lat:local.latitude||'',
//                    lng:local.longitude||'',
//                    g_id:$('#choose').attr('data-id'),
//                    dateTime:dateTime
//                },
//                function(res){
//                    var data = JSON.parse(res);
//                    console.log(data);
//                    if(data.length<1) {
//                        $.toast('无可选技师，请选择其他时间段','text');
//                        setTimeout(function(){
//                            $(self).prev().click();
//                        },1500);
//                        return false;
//                    }
//                    var personnelHtml="";
//                    for (var i=0;i<data.length;i++ ) {
//                        var juli='';
//                        if(Number(data[i].distance)>2000){
//                            juli=(Number(data[i].distance)/1000).toFixed(2)+'公里';
//                        }else{
//                            juli=data[i].distance+'米';
//                        }
//                        personnelHtml+='<li><i onclick="choosePersonnel(this)" class="weui-icon-success" data-id='+data[i].saler.id+' data-name='+data[i].saler.nickname+'></i>' +
//                        '<div><div class="lists_img"><img src='+data[i].saler.avatar+' alt=""/>' +
//                        '</div>' +
//                        '<div class="lists_detail"><p class="detail_name"><span><b>'+data[i].saler.nickname+'</b></span><span>' +
//                        '<i class="iconal-location"></i>'+juli+'</p>' +
//                        '<p class="detail_address"><span><span style="margin-right: 10px">'+data[i].saler.sex+'</span><i class="iconal-wodedingdan"></i><span>200</span>单</span><span>门店: <span>'+data[i].store.storename+'</span></span></p>' +
//                        '<p class="detail_details">'+data[i].saler.content+'</p></div></div></li>';
//                    }
//                    $('#chooseJS ul').html(personnelHtml);
//                    $('#chooseJS').slideDown();
//                }
//        );
//    }
//    ///**选择技师 按钮 icon**/
//    function choosePersonnel(e){
//        $('#chooseJS li i').removeClass('active');
//        $(e).addClass('active');
//    }
//    $('#chooseJS .storeTJ').click(function(e){
//        var jishi=$('#chooseJS li i');
//        for(var i=0;i<jishi.length;i++){
//            if($(jishi[i]).hasClass('active')){
//                $('#show-actions-fuwuJS').attr('data-jishiid',$(jishi[i]).attr('data-id')).find('p').html('技师: '+$(jishi[i]).attr('data-name'));
//                $('#chooseJS').slideUp();
//                return false;
//            }
//        }
//        $.toast('请选择门店','text');
//    });


//加入购物车按钮
function addCart(e, order) {
    //        if($('.MDorSM p').html()=='请填写地址信息'){
    //            $.toast('请先填写地址信息','text');
    //            setTimeout(function(){
    //                $('.MDorSM>a').click();
    //            },1500);
    //            return false;
    //        }
    if (!$('.ischecked').is(':checked')) {
        $.toast('请先同意报名须知协议', 'text');
        return false;
    }else{
        if ($('.MDorSM p').html() == '请选择地点') {
            $.toast('请先选择地点', 'text');
            setTimeout(function() {
                $('.MDorSM>a').click();
            }, 1500);
            return false;
        }
        if ($('#show-actions-times p').html() == '请选择时间') {
            $.toast('请先选择时间', 'text');
            setTimeout(function() {
                $('#show-actions-times').click();
            }, 1500);
            return false;
        }
    }
    //        if($('#show-actions-fuwuJS p').html()=='请选择服务技师'){
    //            $.toast('请先选择服务技师','text');
    //            setTimeout(function(){
    //                $('#show-actions-fuwuJS').click();
    //            },1500);
    //            return false;
    //        }
    // var str = sessionStorage.getItem('sessCart') || '';
    

    var re = $('#choose').attr('data-id') + ($('.MDorSM>a').attr('data-storesid') || '0') + ($('.MDorSM>a').attr('data-id') || '0') +
        (new Date()).getFullYear() + '-' + ($('#show-actions-times').attr('data-day') || '') + ' ' +
        ($('#show-actions-times').attr('data-startt') || '') + $('#show-actions-fuwuJS').attr('data-jishiid');
    var patt1 = new RegExp(re);
    // var result = patt1.test(str);

    // if (result) {
    //     $.toast('购物车已存在该项目', 'text');
    //     return false;
    // }
    var _Time = $('#chooseTime').attr('data-day')
    var _Hours = $('#chooseTime').attr('data-time');
    $.get(clearCartList,function(data){      
    $.get(
        addCar, {
            id: $('#choose').attr('data-id'),
            num: 1,
            storeid: $('.MDorSM>a').attr('data-storesid') || '',
            addressid: $('.MDorSM>a').attr('data-id') || '',
            timeS: (new Date()).getFullYear() + '-' + _Time + ' ' + _Hours.split('-')[0],
            timeE: (new Date()).getFullYear() + '-' + _Time + ' ' + _Hours.split('-')[1],
            jishiid: $('#show-actions-fuwuJS').attr('data-jishiid')
        },
        function(res) {
            if (res == 1) {
                if (order == 'order') {
                    updateCart();
                    setTimeout(function() {
                        location.href = urlorder;
                    }, 500);
                }
                $('#choose').fadeOut();
                updateCart();
            } else {
                $.toast('加入购物车失败', 'text');
            }
        });
    });
}

/*点击预约按钮*/
function yuebiaoClick(a) {
   $.get(member_blacklist_view,function (res) {
        var data = JSON.parse(res);
        if(data.status === 1) {
            if(!data.blacklist == 1){
                $.post(member_favorabler_add,
                {
                    favorable_id: getQueryString('id')
                },function (res) {
                    $('#choose').fadeIn("slow");
                })
            }else{
                $.toast('您被限制预约','text');
                return false;
            }
        }
    })
    var priceVal = $(a).parent().find('b').text();
    
    $('#choose .weui-input').attr('placeholder',priceVal);
//        if (Number($('.cartSum_num>p:last-child>span').html()) >= 360) {
//            $.toast('您预约时间已超过6小时', 'cancel');
//            return false;
//        }

    $('#show-actions-times p').html('请选择时间');

    var dataType = $(a).attr('data-type');
    var MDorSMhtml = '';
    MDorSMhtml = '<a onclick="storesCH(this)" class="weui-cell weui-cell_access" href="javascript:;">' +
    '<div class="weui-cell__bd weui-cell_primary"><p>请选择地点</p></div><span class="weui-cell__ft"></span></a>';
    $('.MDorSM').html(MDorSMhtml);
    $('.text').html(escapeHtml($(a).attr('data-notice')));
    $('#choose').attr('data-id', $(a).attr('data-id'))
        .attr('data-type', $(a).attr('data-type'))
        .attr('data-gnum',$(a).attr('data-gnum'));
}
function escapeHtml(text) {
    var a=text.replace(/(\&lt;)/g,"<");
    var b=a.replace(/(\&gt;)/g,">");
    var c=b.replace(/(\&amp;)/g,"\&");
    var d=c.replace(/(\&quot;)/g,"\"");
    return d;
}
function formatDate (ns) {   //转换时间戳
   var ns = new Date(parseInt(ns) * 1000);
   var Year  = ns.getFullYear();
   var Month = ns.getMonth()+1;
   var DateDay = ns.getDate();
   var Hour = ns.getHours();
   var Minute = ns.getMinutes().toString().length == 1? '0'+ns.getMinutes() : ns.getMinutes();
   return Year+'/'+Month+'/'+DateDay+' '+Hour+':'+Minute; 
}
















